/*
 * license-updater - Copyright (c) 2012 MSF. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */
package br.msf.commons.netbeans.util;

import br.msf.commons.constants.JavaLanguageConstants;
import br.msf.commons.lang.EnhancedStringBuilder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import org.apache.commons.lang.ArrayUtils;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.queries.FileEncodingQuery;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;

/**
 * <p>TODO
 *
 * @author Marcius da Silva da Fonseca (sf.marcius@gmail.com)
 * @version 1.0
 * @since license-updater-1.0
 */
public abstract class FileObjectUtils implements JavaLanguageConstants {

    /**
     * Informa se o FileObject dado representa ou não um pacote válido.
     */
    public static boolean isValidDir(final FileObject pkg) {
        if (pkg != null && pkg.isValid() && pkg.isFolder() && !FileUtil.toFile(pkg).isHidden()) {
            return true;
        }
        return false;
    }

    /**
     * Informa se o FileObject dado representa ou não um arquivo válido.
     */
    public static boolean isValidFile(final FileObject pkg) {
        if (pkg != null && pkg.isValid() && !pkg.isFolder() && !FileUtil.toFile(pkg).isHidden()) {
            return true;
        }
        return false;
    }

    public static FileObject getPackageFileObject(final ClassPath classPath, final String fullPackageName) {
        for (FileObject root : classPath.getRoots()) {
            FileObject fo = getPackageFileObject(root, fullPackageName);
            if (fo != null) {
                return fo;
            }
        }
        return null;
    }

    public static FileObject getPackageFileObject(final FileObject classPathRoot, final String fullPackageName) {
        assert FileObjectUtils.isValidDir(classPathRoot);
        final String pkg_dir = new EnhancedStringBuilder(fullPackageName).replacePattern(PKG_SEPARATOR, DIR_SEPARATOR).toString();
        return classPathRoot.getFileObject(pkg_dir);
    }

    public static Collection<FileObject> getFiles(final FileObject rootDir, final boolean recursive, final FileType... types) {
        assert rootDir != null && rootDir.isFolder() && !ArrayUtils.contains(types, FileType.OTHER);
        Collection<FileObject> fileObjects = new ArrayList<FileObject>();
        Enumeration<FileObject> entries = (Enumeration<FileObject>) rootDir.getChildren(recursive);
        while (entries.hasMoreElements()) {
            FileObject curr = entries.nextElement();
            if (curr.isData()) {
                FileType t = FileType.parse(curr.getExt());
                if (ArrayUtils.isEmpty(types) || ArrayUtils.contains(types, t)) {
                    fileObjects.add(curr);
                }
            }
        }
        return fileObjects;
    }

    public static Charset getEncoding(final FileObject file) {
        return FileEncodingQuery.getEncoding(file);
    }
}
